package reflect;

import annotation.AutoRunMethod;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 访问注解参数
 */
public class ReflectDemo11 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //获取Person类上sleep上的注解@AutoRunMethod中的参数值
        Class cls = Class.forName("reflect.Person");
        Object object = cls.newInstance();
        Method method = cls.getDeclaredMethod("sayhi");
        //确保该方法有@AutoRunMethod注解
        if (method.isAnnotationPresent(AutoRunMethod.class)){
            AutoRunMethod arm = method.getAnnotation(AutoRunMethod.class);
            int value =arm.value();//获取value参数的值
            System.out.println("注解参数值为:"+value);
            for (int i=0;i<value;i++){
                method.invoke(object);
            }
        }
    }
}
